package com.hexiang.service.impl;

import com.hexiang.client.UserHttpApiClient;
import com.hexiang.domain.CommonResponse;
import com.hexiang.domain.User;
import com.hexiang.exception.ClientException;
import com.hexiang.service.UserHttpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;

@Service
@Slf4j
public class UserHttpServiceImpl implements UserHttpService {

    @Resource
    private UserHttpApiClient userHttpApiClient;

    @Override
    public CommonResponse<Serializable> getUsers() {
        try {
            return userHttpApiClient.getUsers();
        } catch (ClientException e) {
            return CommonResponse.builder().code(e.getCode()).msg(e.getMessage()).succ(false).build();
        }
    }

    @Override
    public CommonResponse<Serializable> getUserByName(String name) {
        try {
            return userHttpApiClient.getUserByName(name);
        } catch (ClientException e) {
            return CommonResponse.builder().code(e.getCode()).msg(e.getMessage()).succ(false).build();
        }
    }

    @Override
    public CommonResponse<Serializable> addUser(String name, int age) {
        try {
            return userHttpApiClient.addUser(name, age);
        } catch (ClientException e) {
            return CommonResponse.builder().code(e.getCode()).msg(e.getMessage()).succ(false).build();
        }
    }

    @Override
    public CommonResponse<Serializable> deleteUserById(Long id) {
        try {
            return userHttpApiClient.deleteUserById(id);
        } catch (ClientException e) {
            return CommonResponse.builder().code(e.getCode()).msg(e.getMessage()).succ(false).build();
        }
    }

    @Override
    public CommonResponse<Serializable> updateUser(User user) {
        try {
            return userHttpApiClient.updateUser(user);
        } catch (ClientException e) {
            return CommonResponse.builder().code(e.getCode()).msg(e.getMessage()).succ(false).build();
        }
    }

    @Override
    public CommonResponse<Serializable> getUserPage(Integer pageNum, Integer pageSize) {
        try {
            return userHttpApiClient.getUserPage(pageNum, pageSize);
        } catch (ClientException e) {
            return CommonResponse.builder().code(e.getCode()).msg(e.getMessage()).succ(false).build();
        }
    }

    @Override
    public CommonResponse<Serializable> errTest() {
        try {
            return userHttpApiClient.errTest();
        } catch (ClientException e) {
            return CommonResponse.builder().code(e.getCode()).msg(e.getMessage()).succ(false).build();
        }
    }
}